package DFS_113;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import LCommon.TreeNode;

public class Solution {
	public List<List<Integer>> pathSum(TreeNode root, int sum) {
		List<List<Integer>> res = new LinkedList<List<Integer>>();
		if (root == null) {
			return res;
		}
		Deque<Integer> path = new ArrayDeque<Integer>();
		DFS(root, sum, res, path);
		return res;

	}

	private void DFS(TreeNode root, int sum, List<List<Integer>> res, Deque<Integer> path) {
		// TODO Auto-generated method stub
		if (root == null) {
			return;
		}
		path.add(root.val);
		sum -= root.val;
		if (root.left == null && root.right == null && sum == 0) {
			res.add(new ArrayList<Integer>(path));
			return;
		}
		if (root.left!=null) {
			DFS(root.left, sum, res, path);
			path.removeLast();
		}
		if (root.right!=null) {
			DFS(root.right, sum, res, path);
			path.removeLast();
		}
	}
}
